<?php
namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Wechat\Controller as BaseController;
use App\Models\Advice;
use App\Models\Apply;
use App\Models\Member;
use App\Models\OtherLogin;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

/**
 * 首页
 *
 * @author Miluo Huang
 */
class HomeController extends BaseController
{

    /**
     * 微信首页
     */
    public function home()
    {
    }

    /**
     * 建议
     */
    public function advice(Request $request)
    {
        return view('wechat.home.advice');
    }

    /**
     * 提交建议
     */
    public function saveAdvice(Request $request)
    {
        // 验证输入。
        $this->validate($request, [
            'content' => 'required'
        ], [
            'content.required' => '请填写建议内容！'
        ]);
        $data          = new Advice();
        $data->content = $request->input('content');
        $data->from    = 1;
        $data->save();

        return response()->json([
            'status'  => 1,
            'message' => ''
        ]);
    }

    /**
     * 申请
     */
    public function apply(Request $request){
        // 门店
        $store = Store::where('status',1)->get();
        return view('wechat.home.apply')->withStore($store);
    }

    /**
     * 分享
     */
    public function share(Request $request){
        $wechat_user = Session::get('wechat_user');
        $other_login = OtherLogin::where('type', 2)->where('open_id', $wechat_user['openid'])->first();
        if (is_null($other_login)) {
            //301跳转m端
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.route('WechatRegister'));
            die;
        }
        $text = route('WechatProductList');
        $member = null;
        if(Session::has('user')){
            $member = Session::get('user');
        }
        return view('wechat.home.share')->withText($text)->withUser($member);
    }

    /**
     * 提交申请
     */
    public function saveApply(Request $request){
        // 验证输入。
        $this->validate($request, [
            'mobile' => [
                'required',
                'mobile'
            ],
            'name' => [
                'required'
            ],
//            'quota_money' => [
//                'required'
//            ],
//            'store_id' => [
//                'required'
//            ]
        ],[
            'mobile.required' => '请填写手机号',
            'mobile.mobile' => '请填写正确格式的手机号',
            'name.required' => '请填写姓名',
//            'quota_money.required' => '请填写申请额度',
//            'store_id.required' => '请选择门店',
        ]);

        $member = Member::where('mobile',$request->input('mobile'))->first();
        if(is_null($member)){
            $member = new Member();
            $member->nickname = $request->input('name');
            $member->mobile = $request->input('mobile');
            $member->password  = $request->input('mobile');
            $member->save();
        }

        $from_member = null;
        if($request->has('from_member_id')){
            $from_member = Member::where(\DB::raw('MD5(`id`)'),$request->input('from_member_id'))->first();
        }

        $apply = new Apply();
        $apply->name = $request->input('name');
        $apply->mobile = $request->input('mobile');
        $apply->member_id = $member->id;
        $apply->store_id = $request->has('store_id') ? $request->input('store_id') : 0;
        $apply->quota_money = $request->has('quota_money') ? $request->input('quota_money') : 0;
        $apply->product_id = $request->has('product_id') ? $request->input('product_id') : 0;
        $apply->from_member_id = !is_null($from_member) ? $from_member->id : 0;
        $apply->save();

        return response()->json([
            'status' => 1,
            'message' => '申请成功'
        ]);
    }




}
